﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Numerics;
using RRRSRoguelike;
using RRRSRoguelike.Managers;
using RRRSRoguelike.Helpers;
using RRRSRoguelikeData;

namespace RRRSRoguelike.Entities
{
    /// <summary>
    /// Represents the current sole bad guy.
    /// </summary>
    public class Monster : Creature
    {

        public bool hasMoved;

        #region constructor
        /// <summary>
        /// Takes a point and initializes the monster.
        /// </summary>
        /// <param name="p"></param>
        public Monster(Point position)
            : base()
        {

            this.TileData = TileData.ReadTileDataFromXML(Properties.Settings.Default.CustomFolder + @"\Creatures\Monster.xml");
            this.Position = position;
        }
        #endregion

        #region methods
        //The monster should be in charge of where it moves.
        //This should be a behaviour that can be used by any creature
        /// <summary>
        /// 
        /// </summary>
        /// <param name="playerLocation"></param>
        /// <param name="monsters"></param>
        public void MoveToPlayer(Point playerLocation,IList<Monster> monsters)
        {
            Point temp = Position ;
            
            //move on x axis toward player
            if (X != playerLocation.X)
                temp.X += (X > playerLocation.X) ? -1 : 1;
            //move on y axis toward player
            if (Y != playerLocation.Y)
                temp.Y += (Y > playerLocation.Y) ? -1 : 1;

            DungeonLevel.ClampToDungeon(temp);

            // m stands for Monster in monsters. Prevents monsters from stacking(moving onto the same square).
            if (!monsters.Any(m=>m.X == temp.X && m.Y == temp.Y))
            {
                hasMoved = true;
                Position = temp;
            }
            else if (!monsters.Any(m => m.X == temp.X && m.Y == Position.Y))
            {
                //this resets the temp move to the orginal position resulting in no move on the x axis
                temp.Y = Position.Y;
                hasMoved = true;
                Position = temp;
            }
            else if (!monsters.Any(m => m.X == Position.X && m.Y == temp.Y))
            {
                //this resets the temp move to the orginal position resulting in no move on the y axis
                temp.X = Position.X;
                hasMoved = true;
                Position = temp;
            }
            else
            {
                if(X == temp.X && Y == temp.Y || monsters.Where(m => m.Position == temp).First().hasMoved)
                    hasMoved = true;
            }


            //Currently not used
            //this.CalculateBestMove(DungeonManager.Instance.CreatureManager.Player.Position);
        }
#endregion

    }
}
